﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.Data;

namespace LS.FrmUI.Dal
{
    public class UserInfoDal
    {
        /// <summary>
        /// 用户登陆
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="passWord"></param>
        /// <returns></returns>
        public bool UserLogin(string userName, string passWord)
        {
            string sql = "select * from userinfo where username = @username and password = @password ";
            object o = DBHelper.ExecuteScalar(sql, new SQLiteParameter("@username", userName), new SQLiteParameter("@password", passWord));
            if (string.IsNullOrEmpty(Convert.ToString(o)))
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllUser()
        {
            return DBHelper.GetTable("select * from userinfo");
        }
        /// <summary>
        /// 根据用户名获取用户角色
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public string GetRoleByUserName(string userName)
        {
            string sql = "select UserRole from userinfo where username = @username";
            return Convert.ToString(DBHelper.ExecuteScalar(sql, new SQLiteParameter("@username", userName)));
        }
        /// <summary>
        /// 根据用户名获取密码
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public string GetPwdByUserName(string userName)
        {
            string sql = "select password from userinfo where username = @username";
            return Convert.ToString(DBHelper.ExecuteScalar(sql, new SQLiteParameter("@username", userName)));
        }

        /// <summary>
        /// 更改用户密码
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public bool UpdateUserPwd(string userName,string password)
        {
            string sql = "update userinfo set  password =@password  where username = @username";
            return DBHelper.ExecuteNonQuery(sql, new SQLiteParameter("@password", password), new SQLiteParameter("@username", userName)) > 0;
        }
        
    }
}
